package com.opc.service.impl;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.opc.entity.Device;
import com.opc.model.DeviceExcel;
import com.opc.service.DeviceService;
import com.opc.utils.DeviceCodeUtil;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author SEG
 * TODO 存在部分读取问题
 */
public class ExcelLisener extends AnalysisEventListener<DeviceExcel> {

    private DeviceService deviceService;
    private List<Device> list = new ArrayList<>();

    public ExcelLisener() {
        // 这里是demo，所以随便new一个。实际使用如果到了spring,请使用下面的有参构造函数
        deviceService = new DeviceServiceImpl();
    }


    public ExcelLisener(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    @Override
    public void invoke(DeviceExcel d, AnalysisContext analysisContext) {
        Device device = new Device();
        BeanUtils.copyProperties(d, device);
        device.setCreateUser(1);
        device.setDeviceCode(DeviceCodeUtil.DeviceNumber());
        device.setStatus(0);
        device.setIsDelete(0);
        device.setCreateTime(new Date());
        list.add(device);

    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//        deviceService.saveOrUpdateBatch(list);
    }
}